<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Vue与React18的区别深度解析 | 七里香</title><meta name="author" content="william"><meta name="copyright" content="william"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="章节介绍小伙伴大家好，本章将学习React18与Vue3全方面对比 - React与TS结合开发。 本章学习目标本章将全面对比React与Vue两大框架的相似点和差异点，通过对比的方式，可以让同学们更加快速的掌握两大框。 详细讲解React与TS配合使用，包括路由、状态管理等TS配合。 课程安排 React18与Vue3对比之编程风格与视图风格 React18与Vue3对比之组件-路由-状态管理等">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue与React18的区别深度解析">
<meta property="og:url" content="http://yokeney.gitee.io/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/index.html">
<meta property="og:site_name" content="七里香">
<meta property="og:description" content="章节介绍小伙伴大家好，本章将学习React18与Vue3全方面对比 - React与TS结合开发。 本章学习目标本章将全面对比React与Vue两大框架的相似点和差异点，通过对比的方式，可以让同学们更加快速的掌握两大框。 详细讲解React与TS配合使用，包括路由、状态管理等TS配合。 课程安排 React18与Vue3对比之编程风格与视图风格 React18与Vue3对比之组件-路由-状态管理等">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yokeney.gitee.io/my-hexo-blog/img/vue_vs-react.png">
<meta property="article:published_time" content="2023-04-24T08:39:32.000Z">
<meta property="article:modified_time" content="2023-04-24T08:43:54.928Z">
<meta property="article:author" content="william">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yokeney.gitee.io/my-hexo-blog/img/vue_vs-react.png"><link rel="shortcut icon" href="/my-hexo-blog/img/index.ico"><link rel="canonical" href="http://yokeney.gitee.io/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-hexo-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-hexo-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-hexo-blog/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Vue与React18的区别深度解析',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-24 16:43:54'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-hexo-blog/img/2.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-hexo-blog/img/vue_vs-react.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-hexo-blog/">七里香</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-hexo-blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/vueResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> vue</span></a></li><li><a class="site-page child" href="/my-hexo-blog/reactResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> react</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-shenghuo"></i><span> 后台</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/my-hexo-blog/nodeResources/"><i class="fa-fw iconfont icon-fenlei1"></i><span> node</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Vue与React18的区别深度解析</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-04-24T08:39:32.000Z" title="发表于 2023-04-24 16:39:32">2023-04-24</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-24T08:43:54.928Z" title="更新于 2023-04-24 16:43:54">2023-04-24</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Vue与React18的区别深度解析"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="章节介绍"><a href="#章节介绍" class="headerlink" title="章节介绍"></a>章节介绍</h1><p>小伙伴大家好，本章将学习React18与Vue3全方面对比 - React与TS结合开发。</p>
<h2 id="本章学习目标"><a href="#本章学习目标" class="headerlink" title="本章学习目标"></a>本章学习目标</h2><p>本章将全面对比React与Vue两大框架的相似点和差异点，通过对比的方式，可以让同学们更加快速的掌握两大框。</p>
<p>详细讲解React与TS配合使用，包括路由、状态管理等TS配合。</p>
<h2 id="课程安排"><a href="#课程安排" class="headerlink" title="课程安排"></a>课程安排</h2><ul>
<li>React18与Vue3对比之编程风格与视图风格</li>
<li>React18与Vue3对比之组件-路由-状态管理等风格</li>
<li>React18与Vue3对比之模板-样式-事件-表单等功能</li>
<li>React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能</li>
<li>React18与Vue3对比之diff算法的相同策略与不同策略</li>
<li>React18与Vue3对比之响应式-生命周期-副作用等功能</li>
<li>React与TS配合之基础props限制</li>
<li>React与TS配合之children与event限制</li>
<li>React与TS配合之style与component限制</li>
<li>React与TS配合之use函数限制</li>
<li>React与TS配合之类组件类型限制</li>
<li>React Router路由如何使用TS进行开发</li>
<li>Redux Toolkit状态管理如何使用TS进行开发</li>
</ul>
<h1 id="React18与Vue3对比之编程风格与视图风格"><a href="#React18与Vue3对比之编程风格与视图风格" class="headerlink" title="React18与Vue3对比之编程风格与视图风格"></a>React18与Vue3对比之编程风格与视图风格</h1><h2 id="编程风格对比"><a href="#编程风格对比" class="headerlink" title="编程风格对比"></a>编程风格对比</h2><p>React语法少，难度大</p>
<p>Vue语法多，难度小</p>
<p>例如：在react中是没有指令的，所有的功能都要通过原生JavaScript来实现。所以说当使用Vue框架的时候，真的就是在操作Vue，而使用React框架其实更贴近于原生JS操作。</p>
<p>举一个实例：比如表单操作的时候，在Vue中可以直接通过v-model指定进行操作。而React则比较麻烦，需要通过受控组件的方式给表单添加value和onChange配置来进行操作。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">v-model</span>=<span class="string">&quot;username&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- react --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">value</span>=<span class="string">&#123;username&#125;</span> <span class="attr">onChange</span>=<span class="string">&#123;(ev)</span>=&gt;</span> setUsername(ev.target.value)&#125; /&gt;</span><br></pre></td></tr></table></figure>

<h2 id="视图风格对比"><a href="#视图风格对比" class="headerlink" title="视图风格对比"></a>视图风格对比</h2><p>Vue采用<code>&lt;template&gt;</code>字符串模板。更贴近HTML，学习成本低，但有时候不灵活。</p>
<p>React采用JSX语法，限制比较多，但是可以跟模板语法很好的进行结合。</p>
<p>所以两个框架各有优缺点。</p>
<p>下面看一下JSX灵活的地方。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- vue --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h1</span> <span class="attr">v-if</span>=<span class="string">&quot;leave === 1&quot;</span>&gt;</span></span><br><span class="line">        标题1</span><br><span class="line">    <span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">h2</span> <span class="attr">v-if</span>=<span class="string">&quot;leave === 2&quot;</span>&gt;</span></span><br><span class="line">        标题2</span><br><span class="line">    <span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- react --&gt;</span><br><span class="line"><span class="keyword">let</span> <span class="title function_">Welcome</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> leave = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">const</span> <span class="title class_">Tag</span> = <span class="string">&#x27;h&#x27;</span> + leave;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">    	<span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        	&#123; <span class="tag">&lt;<span class="name">Tag</span>&gt;</span>标题&#123;leave&#125;<span class="tag">&lt;/<span class="name">Tag</span>&gt;</span> &#125;</span></span><br><span class="line"><span class="language-xml">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="React18与Vue3对比之组件-路由-状态管理等风格"><a href="#React18与Vue3对比之组件-路由-状态管理等风格" class="headerlink" title="React18与Vue3对比之组件-路由-状态管理等风格"></a>React18与Vue3对比之组件-路由-状态管理等风格</h1><h2 id="组件风格对比"><a href="#组件风格对比" class="headerlink" title="组件风格对比"></a>组件风格对比</h2><p>Vue采用选项式API，组合式API</p>
<p>React采用类组件，函数组件</p>
<p>两个框架的组件风格差异还是蛮大的，但是仔细分析会发现，其实组合式API和函数组合是非常相近的，都是面向函数进行编程的，比面向对象编程会更加的简单，而且也更加的灵活。</p>
<p>组件都是采用use函数的方式进行功能复用。</p>
<h2 id="路由对比"><a href="#路由对比" class="headerlink" title="路由对比"></a>路由对比</h2><p>Vue语法更加简练</p>
<p>React的use函数太多，不够统一化</p>
<p>这两个框架对于常见的路由需求都是可以灵活实现的，例如：路由表、嵌套、动态、编程式、守卫等。</p>
<p>所以总结来说，Vue的路由使用起来会更加的灵活；React使用起来会略显复杂。并且Vue功能更加丰富，而React有些需求需要自己模拟去实现。</p>
<h2 id="状态管理对比"><a href="#状态管理对比" class="headerlink" title="状态管理对比"></a>状态管理对比</h2><p>Vue采用Vuex或Pinia</p>
<p>React采用Redux或Mobx</p>
<p>Vue中推荐使用Pinia，因为使用更加简单，并且跟Vue3的组合式配合会更加的统一。</p>
<p>React中推荐使用Redux的扩展版本，Redux-toolkit(RTK)，使用起来跟Vue对比还是略显复杂一些，不过常见的功能都可以实现的很好：共享状态、同步、异步、模块化、持久化等。</p>
<pre><code># React18与Vue3对比之模板-样式-事件-表单等功能
</code></pre>
<h2 id="模板对比"><a href="#模板对比" class="headerlink" title="模板对比"></a>模板对比</h2><p>Vue的视图变化，主要通过：指令 + 模板的方式</p>
<p>React的视图变化，主要通过：原生JS + 模板的方式</p>
<p>React的模板比较强大，因为可以编写JSX结构，所以可以做出更加灵活的结构处理。</p>
<h2 id="样式对比"><a href="#样式对比" class="headerlink" title="样式对比"></a>样式对比</h2><p>Vue的class和style都有三种写法：字符串、数组、对象</p>
<p>React的style只能写对象，class只能字符串，可借助classnames这个库</p>
<p>两个框架基本上都可以满足常见的样式需求。</p>
<h2 id="事件对比"><a href="#事件对比" class="headerlink" title="事件对比"></a>事件对比</h2><p>Vue事件功能丰富</p>
<p>React事件传参需要高阶处理</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- Vue --&gt;</span><br><span class="line">&lt;template&gt;</span><br><span class="line">    &lt;ul&gt;</span><br><span class="line">        &lt;li v-for=&quot;item,index in list&quot; @click=&quot;handleClick(index)&quot;&gt;&lt;/li&gt;</span><br><span class="line">    &lt;/ul&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">methods: &#123;</span><br><span class="line">	handleClick(index)&#123;</span><br><span class="line">	</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- <span class="title class_">React</span> --&gt;</span><br><span class="line"><span class="language-xml"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">&#123; </span></span><br><span class="line"><span class="language-xml">    list.map((v, i)=&gt; <span class="tag">&lt;<span class="name">li</span> <span class="attr">onClick</span>=<span class="string">&#123;handleClick(i)&#125;</span>&gt;</span><span class="tag">&lt;/<span class="name">li</span>&gt;</span>)</span></span><br><span class="line"><span class="language-xml">&#125;</span></span><br><span class="line"><span class="language-xml"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">handleClick</span> = (<span class="params">index</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(index)</span><br><span class="line">    &#125;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="表单对比"><a href="#表单对比" class="headerlink" title="表单对比"></a>表单对比</h2><p>Vue表单双向绑定v-model</p>
<p>React表单受控与非受控</p>
<p>针对表单操作这一块来说，Vue的表单指令v-model还是非常灵活的，总体对比要比React使用方便且灵活。</p>
<h1 id="React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能"><a href="#React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能" class="headerlink" title="React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能"></a>React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能</h1><h2 id="组件通信对比"><a href="#组件通信对比" class="headerlink" title="组件通信对比"></a>组件通信对比</h2><p>Vue父子props，子父emits</p>
<p>React父子props，子父回调函数</p>
<p>emits自定义事件和回调函数，但实际上是一样的思想。</p>
<p>跨组件的通信方案也很类似，都是一种依赖注入的方式来实现的。</p>
<h2 id="功能复用处理"><a href="#功能复用处理" class="headerlink" title="功能复用处理"></a>功能复用处理</h2><p>Vue选项式采用：mixins混入；组合式采用：use函数</p>
<p>React类组件采用：Render Props、HOC；函数组件：use函数</p>
<p>可以发现组合式API和函数组件都是采用use函数，所以基本复用是差不多的思想，这也是两个框架推荐的用法。</p>
<h2 id="内容分发处理"><a href="#内容分发处理" class="headerlink" title="内容分发处理"></a>内容分发处理</h2><p>Vue通过<code>&lt;slot&gt;</code>插槽，进行接收</p>
<p>React通过props.children，进行接收</p>
<h2 id="原生DOM处理"><a href="#原生DOM处理" class="headerlink" title="原生DOM处理"></a>原生DOM处理</h2><p>Vue通过ref属性</p>
<p>React也通过ref属性处理</p>
<p>思路都是差不多的，就是给元素添加ref属性，在跟对象或字符串绑定在一起，这样就可以直接获取到DOM元素。</p>
<h1 id="React18与Vue3对比之diff算法的相同策略与不同策略"><a href="#React18与Vue3对比之diff算法的相同策略与不同策略" class="headerlink" title="React18与Vue3对比之diff算法的相同策略与不同策略"></a>React18与Vue3对比之diff算法的相同策略与不同策略</h1><h2 id="diff算法的相同策略"><a href="#diff算法的相同策略" class="headerlink" title="diff算法的相同策略"></a>diff算法的相同策略</h2><p>首先两个框架都是采用了同层级对比的方式，这样可以大大减少对比的次数。</p>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0f72a9f984eb45d8858c0a6d94db9683~tplv-k3u1fbpfcp-watermark.image" alt="18-01-diff算法同层级对比.png"></p>
<p>当两个节点进行对比的时候，要分不同的情况进行处理。</p>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/1686dc540b04446281b25737be3d7e2d~tplv-k3u1fbpfcp-watermark.image" alt="18-02-diff算法对比情况.png"></p>
<p>大部分的策略都是一样的，只有当两个字节都存在子节点的时候，对比方案才有所区别。</p>
<h2 id="diff算法的不同策略"><a href="#diff算法的不同策略" class="headerlink" title="diff算法的不同策略"></a>diff算法的不同策略</h2><p>下面是一个具体的案例，来分别看一下Vue和React是如何进行处理的。</p>
<p><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4c03ca1e020849ff97d3933034381967~tplv-k3u1fbpfcp-watermark.image" alt="18-03-子节点不同的案例.png"></p>
<p>下面先看一下Vue的策略，即数组格式，首尾对比，最长递增子序列。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/e024f297ea18426fb2b2f0bdb8ad49db~tplv-k3u1fbpfcp-watermark.image" alt="18-04-Vue的diff策略.png"></p>
<p>再看一下React的策略，即r链表格式，从左到右，索引比较。</p>
<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/164e013a5aec49e4992405d81423eec2~tplv-k3u1fbpfcp-watermark.image" alt="18-05-React的diff策略.png"></p>
<h1 id="React18与Vue3对比之响应式-生命周期-副作用等功能"><a href="#React18与Vue3对比之响应式-生命周期-副作用等功能" class="headerlink" title="React18与Vue3对比之响应式-生命周期-副作用等功能"></a>React18与Vue3对比之响应式-生命周期-副作用等功能</h1><h2 id="响应式数据对比"><a href="#响应式数据对比" class="headerlink" title="响应式数据对比"></a>响应式数据对比</h2><p>Vue采用响应式数据，底层通过new Proxy()进行监控，灵活性更高</p>
<p>React采用state状态，通过setState()方法进行内部re-render，可控性更强</p>
<h2 id="生命周期对比"><a href="#生命周期对比" class="headerlink" title="生命周期对比"></a>生命周期对比</h2><p>Vue生命周期钩子(常见)</p>
<ul>
<li>beforeCreate</li>
<li>created</li>
<li>beforeMount</li>
<li>mounted</li>
<li>beforeUpdate</li>
<li>updated</li>
<li>beforeUnmount</li>
<li>unmounted</li>
</ul>
<p>React生命周期钩子(常见)</p>
<ul>
<li>constructor</li>
<li>componentDidMount</li>
<li>componentDidUpdate</li>
<li>componentWillUnmount</li>
<li>render</li>
</ul>
<p>整体对比来看，Vue的生命周期会更丰富一些，React生命周期会更简约一些。</p>
<h2 id="副作用处理对比"><a href="#副作用处理对比" class="headerlink" title="副作用处理对比"></a>副作用处理对比</h2><p>vue使用，watchEffect()</p>
<p>react使用，useEffect()</p>
<p>都是处理副作用的方法，用法上还是有很大区别的。</p>
<p>watchEffect会自动根据所依赖的值进行重渲染，而useEffect要明确指定对应的值才能进行重渲染，React团队已经给出在未来的版本中可能会改成根据所依赖的值自动进行重渲染的操作，但暂时还不行。</p>
<p>watchEffect在更新前和卸载前触发的方式是通过回调函数的参数被调用来实现的，而useEffect是通过return的返回值来指定的。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Vue</span></span><br><span class="line"><span class="title function_">watchEffect</span>(<span class="function">(<span class="params">cb</span>)=&gt;</span>&#123;</span><br><span class="line">	<span class="title function_">cb</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">       <span class="comment">//更新前的触发</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// React</span></span><br><span class="line"><span class="title function_">useEffect</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="function">()=&gt;</span>&#123;</span><br><span class="line">      <span class="comment">//更新前的触发</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h1 id="React与TS配合之基础props限制"><a href="#React与TS配合之基础props限制" class="headerlink" title="React与TS配合之基础props限制"></a>React与TS配合之基础props限制</h1><h2 id="对父子通信进行类型限定"><a href="#对父子通信进行类型限定" class="headerlink" title="对父子通信进行类型限定"></a>对父子通信进行类型限定</h2><p>首先让脚手架支持TypeScript，可以在安装脚手架的时候进行配置即可，命令如下。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npx create-react-app react-ts-study --template typescript</span><br></pre></td></tr></table></figure>

<p>然后就是创建两个组件，并且完成props通信。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  msg?: <span class="built_in">string</span></span><br><span class="line">  count?: <span class="built_in">number</span></span><br><span class="line">  <span class="attr">list</span>: <span class="built_in">string</span>[]</span><br><span class="line">  <span class="attr">info</span>: &#123; <span class="attr">username</span>: <span class="built_in">string</span>; <span class="attr">age</span>: <span class="built_in">number</span> &#125;</span><br><span class="line">  status?: <span class="string">&#x27;loading&#x27;</span> | <span class="string">&#x27;success&#x27;</span> | <span class="string">&#x27;error&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; count = <span class="number">0</span> &#125; = props;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome, &#123;count&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>01_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">msg</span>=<span class="string">&quot;hello&quot;</span> <span class="attr">count</span>=<span class="string">&#123;123&#125;</span> <span class="attr">list</span>=<span class="string">&#123;[</span>&#x27;<span class="attr">a</span>&#x27;, &#x27;<span class="attr">b</span>&#x27;, &#x27;<span class="attr">c</span>&#x27;]&#125; <span class="attr">info</span>=<span class="string">&#123;&#123;username:</span> &#x27;<span class="attr">xiaoming</span>&#x27;, <span class="attr">age:</span> <span class="attr">20</span>&#125;&#125; /&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">list</span>=<span class="string">&#123;[</span>&#x27;<span class="attr">a</span>&#x27;, &#x27;<span class="attr">b</span>&#x27;, &#x27;<span class="attr">c</span>&#x27;]&#125; <span class="attr">info</span>=<span class="string">&#123;&#123;username:</span> &#x27;<span class="attr">xiaoming</span>&#x27;, <span class="attr">age:</span> <span class="attr">20</span>&#125;&#125; /&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">status</span>=<span class="string">&quot;loading&quot;</span> <span class="attr">list</span>=<span class="string">&#123;[</span>&#x27;<span class="attr">a</span>&#x27;, &#x27;<span class="attr">b</span>&#x27;, &#x27;<span class="attr">c</span>&#x27;]&#125; <span class="attr">info</span>=<span class="string">&#123;&#123;username:</span> &#x27;<span class="attr">xiaoming</span>&#x27;, <span class="attr">age:</span> <span class="attr">20</span>&#125;&#125; /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>下面来看一下函数表达式写法的情况下，如何指定props的类型，可通过内置的FC类型来进行实现。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title class_">Welcome</span>: <span class="title class_">React</span>.<span class="property">FC</span>&lt;<span class="title class_">WelcomeProps</span>&gt; = <span class="function">(<span class="params">props</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="React与TS配合之children与event限制"><a href="#React与TS配合之children与event限制" class="headerlink" title="React与TS配合之children与event限制"></a>React与TS配合之children与event限制</h1><h2 id="children的类型限制"><a href="#children的类型限制" class="headerlink" title="children的类型限制"></a>children的类型限制</h2><p>父子通信时候的内容分发进行限制。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  children?: <span class="title class_">React</span>.<span class="property">ReactNode</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome, &#123;props.children&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>02_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">        aaaaa</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">Welcome</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>我们把children属性作为可选参数，这样当<Welcome>组件进行内容分发和不进行内容分发都是可以的。</p>
<h2 id="event限制"><a href="#event限制" class="headerlink" title="event限制"></a>event限制</h2><p>event在React中主要通过内置的ev: React.MouseEvent<HTMLButtonElement>来进行限定。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  children?: <span class="title class_">React</span>.<span class="property">ReactNode</span></span><br><span class="line">  handleMsg?: <span class="function">(<span class="params">ev: React.MouseEvent&lt;HTMLButtonElement&gt;</span>)=&gt;</span> <span class="built_in">void</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> <span class="title function_">handleChange</span> = (<span class="params">ev: React.ChangeEvent&lt;HTMLInputElement&gt;</span>) =&gt; &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(ev.<span class="property">target</span>.<span class="property">value</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome, &#123;props.children&#125;<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;props.handleMsg&#125;</span>&gt;</span>点击<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">onChange</span>=<span class="string">&#123;handleChange&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>02_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">handleMsg</span>=<span class="string">&#123;(ev)</span>=&gt;</span>&#123;&#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml">        aaaaa</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;/<span class="name">Welcome</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      </span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="React与TS配合之style与component限制"><a href="#React与TS配合之style与component限制" class="headerlink" title="React与TS配合之style与component限制"></a>React与TS配合之style与component限制</h1><h2 id="style限制"><a href="#style限制" class="headerlink" title="style限制"></a>style限制</h2><p>当我们进行style样式通信的时候，也是可以指定类型，防止样式传递的时候不复合规范。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">HeaderProps</span> &#123;</span><br><span class="line">  <span class="attr">username</span>: <span class="built_in">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  <span class="attr">style</span>: <span class="title class_">React</span>.<span class="property">CSSProperties</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>03_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span>&#x27;<span class="attr">border</span>&#x27;<span class="attr">:</span> &#x27;<span class="attr">1px</span> <span class="attr">red</span> <span class="attr">solid</span>&#x27;, <span class="attr">display:</span> &#x27;<span class="attr">none</span>&#x27;&#125;&#125; /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主要通过React.CSSProperties来指定样式的类型，这样当传递的样式属性或者值不符合规范的时候，就不会产生TS的提示。</p>
<h2 id="component限制"><a href="#component限制" class="headerlink" title="component限制"></a>component限制</h2><p>如果组件进行通信的时候，也可以进行类型的限制。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">HeaderProps</span> &#123;</span><br><span class="line">  <span class="attr">username</span>: <span class="built_in">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  <span class="attr">style</span>: <span class="title class_">React</span>.<span class="property">CSSProperties</span></span><br><span class="line">  <span class="attr">component</span>: <span class="title class_">React</span>.<span class="property">ComponentType</span>&lt;<span class="title class_">HeaderProps</span>&gt;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">props.component</span> <span class="attr">username</span>=<span class="string">&quot;xiaoming&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">props.component</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Header</span>(<span class="params">props: HeaderProps</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>hello Header<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>03_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span>&#x27;<span class="attr">border</span>&#x27;<span class="attr">:</span> &#x27;<span class="attr">1px</span> <span class="attr">red</span> <span class="attr">solid</span>&#x27;, <span class="attr">display:</span> &#x27;<span class="attr">none</span>&#x27;&#125;&#125; <span class="attr">component</span>=<span class="string">&#123;Header&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主要通过React.ComponentType&lt;&gt;来指定组件的类型，那么一旦不符合指定的接口类型，就会报错。</p>
<h1 id="React与TS配合之use函数限制"><a href="#React与TS配合之use函数限制" class="headerlink" title="React与TS配合之use函数限制"></a>React与TS配合之use函数限制</h1><h2 id="use函数限制"><a href="#use函数限制" class="headerlink" title="use函数限制"></a>use函数限制</h2><p>在React函数组件中，主要就是对use函数进行类型的注解。常见的注解use函数如下：</p>
<ul>
<li>useState -&gt; 联合类型、对象字面量类型</li>
<li>useEffect -&gt; 自动类型推断</li>
<li>useRef -&gt; 泛型标签类型</li>
</ul>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, &#123; useEffect, useState, useRef &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Welcome</span>(<span class="params">props: WelcomeProps</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>hello Welcome<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">type</span> <span class="title class_">Info</span> = &#123;<span class="attr">username</span>: <span class="built_in">string</span>; <span class="attr">age</span>: <span class="built_in">number</span>&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="comment">//const [count, setCount] = useState(0)</span></span><br><span class="line">  <span class="keyword">const</span> [count, setCount] = useState&lt;<span class="built_in">number</span>|<span class="built_in">string</span>&gt;(<span class="number">0</span>)</span><br><span class="line">  <span class="keyword">const</span> [list, setList] = useState&lt;<span class="built_in">string</span>[]&gt;([])</span><br><span class="line">  <span class="comment">//const [info, setInfo] = useState&lt;&#123;username: string; age: number&#125;|null&gt;(null)</span></span><br><span class="line">  <span class="keyword">const</span> [info, setInfo] = useState&lt;<span class="title class_">Info</span>&gt;(&#123;&#125; <span class="keyword">as</span> <span class="title class_">Info</span>)</span><br><span class="line">  <span class="keyword">const</span> myRef = useRef&lt;<span class="title class_">HTMLButtonElement</span>&gt;(<span class="literal">null</span>)</span><br><span class="line">  <span class="title function_">useEffect</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>( myRef.<span class="property">current</span>?.<span class="property">innerHTML</span> )  <span class="comment">// 可选链(类型保护)</span></span><br><span class="line">    <span class="comment">//console.log( myRef.current!.innerHTML )  // 非空断言(慎用)   </span></span><br><span class="line">    <span class="keyword">return</span> <span class="function">()=&gt;</span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [])</span><br><span class="line">  <span class="keyword">const</span> <span class="title function_">handleClick</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="title function_">setCount</span>(<span class="number">1</span>)</span><br><span class="line">    <span class="title function_">setList</span>([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>])</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>04_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">onClick</span>=<span class="string">&#123;handleClick&#125;</span> <span class="attr">ref</span>=<span class="string">&#123;myRef&#125;</span>&gt;</span>点击<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      &#123; info.username &#125;, &#123; info.age &#125;</span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> /&gt;</span>      </span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>useState和useRef都是通过泛型的方式进行类型注解，useEffect主要利用自动类型推断来完成。</p>
<h1 id="React与TS配合之类组件类型限制"><a href="#React与TS配合之类组件类型限制" class="headerlink" title="React与TS配合之类组件类型限制"></a>React与TS配合之类组件类型限制</h1><h2 id="类组件类型限定"><a href="#类组件类型限定" class="headerlink" title="类组件类型限定"></a>类组件类型限定</h2><p>类组件在React中并不是重点，但是也要了解怎么对类组件进行类型的限制。</p>
<figure class="highlight tsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="title class_">React</span>, &#123; <span class="title class_">Component</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeProps</span> &#123;</span><br><span class="line">  <span class="attr">msg</span>: <span class="built_in">string</span></span><br><span class="line">  <span class="attr">count</span>: <span class="built_in">number</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">WelcomeState</span> &#123;</span><br><span class="line">  <span class="attr">username</span>: <span class="built_in">string</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Welcome</span> <span class="keyword">extends</span> <span class="title class_ inherited__">Component</span>&lt;<span class="title class_">WelcomeProps</span>, <span class="title class_">WelcomeState</span>&gt; &#123;</span><br><span class="line">  state = &#123;</span><br><span class="line">    <span class="attr">username</span>: <span class="string">&#x27;xiaoming&#x27;</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="title function_">render</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>hello Welcome &#123;this.state.username&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="keyword">function</span> <span class="title function_">App</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">h2</span>&gt;</span>05_react-ts<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">      <span class="tag">&lt;<span class="name">Welcome</span> <span class="attr">msg</span>=<span class="string">&quot;hello&quot;</span> <span class="attr">count</span>=<span class="string">&#123;123&#125;</span> /&gt;</span>      </span></span><br><span class="line"><span class="language-xml">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>主要就是给继承的类Component传递泛型，Props和State，这样可以实现父子通信的数据进行类型限制，又可以对内部的state进行类型限制。</p>
<pre><code># React Router路由如何使用TS进行开发
</code></pre>
<h2 id="react-router-dom类型限制"><a href="#react-router-dom类型限制" class="headerlink" title="react-router-dom类型限制"></a>react-router-dom类型限制</h2><p>React路由与TS配合常见的使用为以下这些操作：</p>
<ul>
<li>RouteObject 内置类型，限制路由表</li>
<li>React.createElement() 进行组件编写</li>
<li>扩展 meta 元信息</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /router/index.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; createBrowserRouter &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; <span class="title class_">RouteObject</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;react-router-dom&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="title class_">App</span> <span class="keyword">from</span> <span class="string">&#x27;../App&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Index</span> <span class="keyword">from</span> <span class="string">&#x27;../views/Index/Index&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">User</span> <span class="keyword">from</span> <span class="string">&#x27;../views/User/User&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">Login</span> <span class="keyword">from</span> <span class="string">&#x27;../views/Login/Login&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="title class_">React</span> <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">declare</span> <span class="variable language_">module</span> <span class="string">&#x27;react-router&#x27;</span> &#123;</span><br><span class="line">  <span class="keyword">interface</span> <span class="title class_">NonIndexRouteObject</span> &#123;</span><br><span class="line">    meta?: &#123; <span class="attr">title</span>: <span class="built_in">string</span> &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">interface</span> <span class="title class_">IndexRouteObject</span> &#123;</span><br><span class="line">    meta?: &#123; <span class="attr">title</span>: <span class="built_in">string</span> &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="attr">routes</span>: <span class="title class_">RouteObject</span>[] = [</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">    <span class="attr">element</span>: <span class="title class_">React</span>.<span class="title function_">createElement</span>(<span class="title class_">App</span>),</span><br><span class="line">    <span class="attr">meta</span>: &#123; <span class="attr">title</span>: <span class="string">&#x27;/&#x27;</span> &#125;,</span><br><span class="line">    <span class="attr">children</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;index&#x27;</span>,</span><br><span class="line">        <span class="attr">element</span>: <span class="title class_">React</span>.<span class="title function_">createElement</span>(<span class="title class_">Index</span>),</span><br><span class="line">        <span class="attr">meta</span>: &#123; <span class="attr">title</span>: <span class="string">&#x27;index&#x27;</span> &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;user&#x27;</span>,</span><br><span class="line">        <span class="attr">element</span>: <span class="title class_">React</span>.<span class="title function_">createElement</span>(<span class="title class_">User</span>),</span><br><span class="line">        <span class="attr">meta</span>: &#123; <span class="attr">title</span>: <span class="string">&#x27;user&#x27;</span> &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">path</span>: <span class="string">&#x27;login&#x27;</span>,</span><br><span class="line">        <span class="attr">element</span>: <span class="title class_">React</span>.<span class="title function_">createElement</span>(<span class="title class_">Login</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">];</span><br><span class="line"><span class="keyword">const</span> router = <span class="title function_">createBrowserRouter</span>(routes);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> router;</span><br></pre></td></tr></table></figure>

<pre><code># Redux Toolkit状态管理如何使用TS进行开发
</code></pre>
<h2 id="Redux-Toolkit限制类型"><a href="#Redux-Toolkit限制类型" class="headerlink" title="Redux Toolkit限制类型"></a>Redux Toolkit限制类型</h2><p>Redux状态管理与TS配合常见的使用为以下这些操作：</p>
<ul>
<li>得到全局state类型： ReturnType<typeof store.getState></li>
<li>限定payload类型： PayloadAction</li>
</ul>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /store/index.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; configureStore &#125; <span class="keyword">from</span> <span class="string">&#x27;@reduxjs/toolkit&#x27;</span></span><br><span class="line"><span class="keyword">import</span> userReducer <span class="keyword">from</span> <span class="string">&#x27;./modules/user&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; useDispatch &#125; <span class="keyword">from</span> <span class="string">&#x27;react-redux&#x27;</span></span><br><span class="line"><span class="keyword">const</span> store = <span class="title function_">configureStore</span>(&#123;</span><br><span class="line">  <span class="attr">reducer</span>: &#123;</span><br><span class="line">    <span class="attr">user</span>: userReducer</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> <span class="title class_">RootState</span> = <span class="title class_">ReturnType</span>&lt;<span class="keyword">typeof</span> store.<span class="property">getState</span>&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> <span class="title class_">AppDispatch</span> = <span class="keyword">typeof</span> store.<span class="property">dispatch</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="attr">useAppDispatch</span>: <span class="function">() =&gt;</span> <span class="title class_">AppDispatch</span> = useDispatch</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// /store/modules/user.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; createSlice, createAsyncThunk &#125; <span class="keyword">from</span> <span class="string">&#x27;@reduxjs/toolkit&#x27;</span></span><br><span class="line"><span class="keyword">import</span> <span class="keyword">type</span> &#123; <span class="title class_">PayloadAction</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@reduxjs/toolkit&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> loginAction = <span class="title function_">createAsyncThunk</span>(</span><br><span class="line">  <span class="string">&#x27;users/loginAction&#x27;</span>,</span><br><span class="line">  <span class="keyword">async</span> (<span class="attr">userId</span>: <span class="built_in">number</span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> response = <span class="keyword">await</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve</span>)=&gt;</span>&#123;</span><br><span class="line">      <span class="title function_">resolve</span>(<span class="string">&#x27;response data&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="keyword">return</span> response</span><br><span class="line">  &#125;</span><br><span class="line">)</span><br><span class="line"><span class="keyword">const</span> userSlice = <span class="title function_">createSlice</span>(&#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&#x27;user&#x27;</span>,</span><br><span class="line">  <span class="attr">initialState</span>: &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;xiaoming&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">reducers</span>: &#123;</span><br><span class="line">    <span class="title function_">change</span>(<span class="params">state, action: PayloadAction&lt;<span class="built_in">string</span>&gt;</span>)&#123;</span><br><span class="line">      state.<span class="property">name</span> = action.<span class="property">payload</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> &#123; change &#125; = userSlice.<span class="property">actions</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> userSlice.<span class="property">reducer</span></span><br></pre></td></tr></table></figure>



</article><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/"><img class="prev-cover" src="/my-hexo-blog/img/1.png" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">史上最全Vue3总结</div></div></a></div><div class="next-post pull-right"><a href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/"><img class="next-cover" src="/my-hexo-blog/img/babel.webp" onerror="onerror=null;src='/my-hexo-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">手把手教你写babel插件</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-hexo-blog/img/2.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">william</div><div class="author-info__description"> 青山不改，绿水长流</div></div><div class="card-info-data site-data is-center"><a href="/my-hexo-blog/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/my-hexo-blog/tags/"><div class="headline">标签</div><div class="length-num">13</div></a><a href="/my-hexo-blog/categories/"><div class="headline">分类</div><div class="length-num">0</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/yokeney"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/yokeney" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/my-hexo-blog/965660471@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">加油</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AB%A0%E8%8A%82%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.</span> <span class="toc-text">章节介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E7%AB%A0%E5%AD%A6%E4%B9%A0%E7%9B%AE%E6%A0%87"><span class="toc-number">1.1.</span> <span class="toc-text">本章学习目标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%BE%E7%A8%8B%E5%AE%89%E6%8E%92"><span class="toc-number">1.2.</span> <span class="toc-text">课程安排</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React18%E4%B8%8EVue3%E5%AF%B9%E6%AF%94%E4%B9%8B%E7%BC%96%E7%A8%8B%E9%A3%8E%E6%A0%BC%E4%B8%8E%E8%A7%86%E5%9B%BE%E9%A3%8E%E6%A0%BC"><span class="toc-number">2.</span> <span class="toc-text">React18与Vue3对比之编程风格与视图风格</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E9%A3%8E%E6%A0%BC%E5%AF%B9%E6%AF%94"><span class="toc-number">2.1.</span> <span class="toc-text">编程风格对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A7%86%E5%9B%BE%E9%A3%8E%E6%A0%BC%E5%AF%B9%E6%AF%94"><span class="toc-number">2.2.</span> <span class="toc-text">视图风格对比</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React18%E4%B8%8EVue3%E5%AF%B9%E6%AF%94%E4%B9%8B%E7%BB%84%E4%BB%B6-%E8%B7%AF%E7%94%B1-%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86%E7%AD%89%E9%A3%8E%E6%A0%BC"><span class="toc-number">3.</span> <span class="toc-text">React18与Vue3对比之组件-路由-状态管理等风格</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E9%A3%8E%E6%A0%BC%E5%AF%B9%E6%AF%94"><span class="toc-number">3.1.</span> <span class="toc-text">组件风格对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E5%AF%B9%E6%AF%94"><span class="toc-number">3.2.</span> <span class="toc-text">路由对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E7%AE%A1%E7%90%86%E5%AF%B9%E6%AF%94"><span class="toc-number">3.3.</span> <span class="toc-text">状态管理对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A8%A1%E6%9D%BF%E5%AF%B9%E6%AF%94"><span class="toc-number">3.4.</span> <span class="toc-text">模板对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%B7%E5%BC%8F%E5%AF%B9%E6%AF%94"><span class="toc-number">3.5.</span> <span class="toc-text">样式对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E5%AF%B9%E6%AF%94"><span class="toc-number">3.6.</span> <span class="toc-text">事件对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%A1%A8%E5%8D%95%E5%AF%B9%E6%AF%94"><span class="toc-number">3.7.</span> <span class="toc-text">表单对比</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React18%E4%B8%8EVue3%E5%AF%B9%E6%AF%94%E4%B9%8B%E7%BB%84%E4%BB%B6%E9%80%9A%E4%BF%A1-%E9%80%BB%E8%BE%91%E5%A4%8D%E7%94%A8-%E5%86%85%E5%AE%B9%E5%88%86%E5%8F%91-DOM%E6%93%8D%E4%BD%9C%E7%AD%89%E5%8A%9F%E8%83%BD"><span class="toc-number">4.</span> <span class="toc-text">React18与Vue3对比之组件通信-逻辑复用-内容分发-DOM操作等功能</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%84%E4%BB%B6%E9%80%9A%E4%BF%A1%E5%AF%B9%E6%AF%94"><span class="toc-number">4.1.</span> <span class="toc-text">组件通信对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8A%9F%E8%83%BD%E5%A4%8D%E7%94%A8%E5%A4%84%E7%90%86"><span class="toc-number">4.2.</span> <span class="toc-text">功能复用处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E5%AE%B9%E5%88%86%E5%8F%91%E5%A4%84%E7%90%86"><span class="toc-number">4.3.</span> <span class="toc-text">内容分发处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8E%9F%E7%94%9FDOM%E5%A4%84%E7%90%86"><span class="toc-number">4.4.</span> <span class="toc-text">原生DOM处理</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React18%E4%B8%8EVue3%E5%AF%B9%E6%AF%94%E4%B9%8Bdiff%E7%AE%97%E6%B3%95%E7%9A%84%E7%9B%B8%E5%90%8C%E7%AD%96%E7%95%A5%E4%B8%8E%E4%B8%8D%E5%90%8C%E7%AD%96%E7%95%A5"><span class="toc-number">5.</span> <span class="toc-text">React18与Vue3对比之diff算法的相同策略与不同策略</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#diff%E7%AE%97%E6%B3%95%E7%9A%84%E7%9B%B8%E5%90%8C%E7%AD%96%E7%95%A5"><span class="toc-number">5.1.</span> <span class="toc-text">diff算法的相同策略</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#diff%E7%AE%97%E6%B3%95%E7%9A%84%E4%B8%8D%E5%90%8C%E7%AD%96%E7%95%A5"><span class="toc-number">5.2.</span> <span class="toc-text">diff算法的不同策略</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React18%E4%B8%8EVue3%E5%AF%B9%E6%AF%94%E4%B9%8B%E5%93%8D%E5%BA%94%E5%BC%8F-%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F-%E5%89%AF%E4%BD%9C%E7%94%A8%E7%AD%89%E5%8A%9F%E8%83%BD"><span class="toc-number">6.</span> <span class="toc-text">React18与Vue3对比之响应式-生命周期-副作用等功能</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%93%8D%E5%BA%94%E5%BC%8F%E6%95%B0%E6%8D%AE%E5%AF%B9%E6%AF%94"><span class="toc-number">6.1.</span> <span class="toc-text">响应式数据对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%AF%B9%E6%AF%94"><span class="toc-number">6.2.</span> <span class="toc-text">生命周期对比</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%AF%E4%BD%9C%E7%94%A8%E5%A4%84%E7%90%86%E5%AF%B9%E6%AF%94"><span class="toc-number">6.3.</span> <span class="toc-text">副作用处理对比</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E4%B8%8ETS%E9%85%8D%E5%90%88%E4%B9%8B%E5%9F%BA%E7%A1%80props%E9%99%90%E5%88%B6"><span class="toc-number">7.</span> <span class="toc-text">React与TS配合之基础props限制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AF%B9%E7%88%B6%E5%AD%90%E9%80%9A%E4%BF%A1%E8%BF%9B%E8%A1%8C%E7%B1%BB%E5%9E%8B%E9%99%90%E5%AE%9A"><span class="toc-number">7.1.</span> <span class="toc-text">对父子通信进行类型限定</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E4%B8%8ETS%E9%85%8D%E5%90%88%E4%B9%8Bchildren%E4%B8%8Eevent%E9%99%90%E5%88%B6"><span class="toc-number">8.</span> <span class="toc-text">React与TS配合之children与event限制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#children%E7%9A%84%E7%B1%BB%E5%9E%8B%E9%99%90%E5%88%B6"><span class="toc-number">8.1.</span> <span class="toc-text">children的类型限制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#event%E9%99%90%E5%88%B6"><span class="toc-number">8.2.</span> <span class="toc-text">event限制</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E4%B8%8ETS%E9%85%8D%E5%90%88%E4%B9%8Bstyle%E4%B8%8Ecomponent%E9%99%90%E5%88%B6"><span class="toc-number">9.</span> <span class="toc-text">React与TS配合之style与component限制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#style%E9%99%90%E5%88%B6"><span class="toc-number">9.1.</span> <span class="toc-text">style限制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#component%E9%99%90%E5%88%B6"><span class="toc-number">9.2.</span> <span class="toc-text">component限制</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E4%B8%8ETS%E9%85%8D%E5%90%88%E4%B9%8Buse%E5%87%BD%E6%95%B0%E9%99%90%E5%88%B6"><span class="toc-number">10.</span> <span class="toc-text">React与TS配合之use函数限制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#use%E5%87%BD%E6%95%B0%E9%99%90%E5%88%B6"><span class="toc-number">10.1.</span> <span class="toc-text">use函数限制</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#React%E4%B8%8ETS%E9%85%8D%E5%90%88%E4%B9%8B%E7%B1%BB%E7%BB%84%E4%BB%B6%E7%B1%BB%E5%9E%8B%E9%99%90%E5%88%B6"><span class="toc-number">11.</span> <span class="toc-text">React与TS配合之类组件类型限制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E7%BB%84%E4%BB%B6%E7%B1%BB%E5%9E%8B%E9%99%90%E5%AE%9A"><span class="toc-number">11.1.</span> <span class="toc-text">类组件类型限定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#react-router-dom%E7%B1%BB%E5%9E%8B%E9%99%90%E5%88%B6"><span class="toc-number">11.2.</span> <span class="toc-text">react-router-dom类型限制</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Redux-Toolkit%E9%99%90%E5%88%B6%E7%B1%BB%E5%9E%8B"><span class="toc-number">11.3.</span> <span class="toc-text">Redux Toolkit限制类型</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="VUE3集成企业级后台通用规范模版"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/05/26/VUE3%E9%9B%86%E6%88%90%E4%BC%81%E4%B8%9A%E7%BA%A7%E5%90%8E%E5%8F%B0%E9%80%9A%E7%94%A8%E8%A7%84%E8%8C%83%E6%A8%A1%E7%89%88/" title="VUE3集成企业级后台通用规范模版">VUE3集成企业级后台通用规范模版</a><time datetime="2023-05-26T13:17:48.000Z" title="发表于 2023-05-26 21:17:48">2023-05-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解"><img src="/my-hexo-blog/img/react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="没看这篇文章之前原来我对react18一知半解"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E6%B2%A1%E7%9C%8B%E8%BF%99%E7%AF%87%E6%96%87%E7%AB%A0%E4%B9%8B%E5%89%8D%E5%8E%9F%E6%9D%A5%E6%88%91%E5%AF%B9react18%E4%B8%80%E7%9F%A5%E5%8D%8A%E8%A7%A3/" title="没看这篇文章之前原来我对react18一知半解">没看这篇文章之前原来我对react18一知半解</a><time datetime="2023-04-24T09:11:02.000Z" title="发表于 2023-04-24 17:11:02">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结"><img src="/my-hexo-blog/img/1.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="史上最全Vue3总结"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E5%8F%B2%E4%B8%8A%E6%9C%80%E5%85%A8Vue3%E6%80%BB%E7%BB%93/" title="史上最全Vue3总结">史上最全Vue3总结</a><time datetime="2023-04-24T09:01:19.000Z" title="发表于 2023-04-24 17:01:19">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析"><img src="/my-hexo-blog/img/vue_vs-react.png" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="Vue与React18的区别深度解析"/></a><div class="content"><a class="title" href="/my-hexo-blog/2023/04/24/%E4%B8%8EReact18%E7%9A%84%E5%8C%BA%E5%88%AB%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90/" title="Vue与React18的区别深度解析">Vue与React18的区别深度解析</a><time datetime="2023-04-24T08:39:32.000Z" title="发表于 2023-04-24 16:39:32">2023-04-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件"><img src="/my-hexo-blog/img/babel.webp" onerror="this.onerror=null;this.src='/my-hexo-blog/img/404.jpg'" alt="手把手教你写babel插件"/></a><div class="content"><a class="title" href="/my-hexo-blog/2022/10/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E5%86%99%E4%B8%80%E4%B8%AAbabel/" title="手把手教你写babel插件">手把手教你写babel插件</a><time datetime="2022-10-25T01:10:14.000Z" title="发表于 2022-10-25 09:10:14">2022-10-25</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-hexo-blog/img/1.png')"><div id="footer-wrap"><div class="copyright">&copy;2018 - 2023 By william</div><div class="footer_custom_text">关山难越，谁悲失路之人</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-hexo-blog/js/utils.js"></script><script src="/my-hexo-blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/my-hexo-blog/js/search/local-search.js"></script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="22712173" data-server="netease" data-mini="true" data-type="song" data-autoplay="false" data-lrcType="-1"> </div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>